package com.tanhua.server.service;

import com.tanhua.dubbo.api.db.BlackListApi;
import com.tanhua.dubbo.api.db.QuestionApi;
import com.tanhua.dubbo.api.db.SettingsApi;
import com.tanhua.dubbo.api.db.UserInfoApi;
import com.tanhua.model.db.*;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.model.vo.UserInfoVoAge;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 通用设置业务处理类层
 */
@Service
public class SettingsService {

    @DubboReference
    private QuestionApi questionApi;

    @DubboReference
    private SettingsApi settingsApi;

    @DubboReference
    private BlackListApi blackListApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    /**
     * 用户通用设置 - 读取
     */
    public SettingsVo settings() {
        User user = UserHolder.getUser();
        Long userId = user.getId();//当前用户id
        //1根据当前用户id调用服务查询陌生人问题
        Question question = questionApi.findByUserId(userId);
        String strangerQuestion = "妹子还没来";//默认值
        //1.1 如果question为空默认问题
        if (question != null && !StringUtils.isEmpty(question.getTxt())) {
            strangerQuestion = question.getTxt();//数据库内容
        }
        //2根据当前用户id调用服务查询通知设置
        Settings settings = settingsApi.findByUserId(userId);
        //3获取手机号号码
        String mobile = user.getMobile();//当前用户手机号
        //4构造vo返回
        SettingsVo settingsVo = new SettingsVo();
        if (settings != null) {
            BeanUtils.copyProperties(settings, settingsVo);//3个通知值copy
        }
        settingsVo.setId(userId);//用户id
        settingsVo.setStrangerQuestion(strangerQuestion);//陌生人问题
        settingsVo.setPhone(mobile);//手机号
        return settingsVo;
    }

    /**
     * 设置陌生人问题 - 保存
     */
    public void saveQuestions(String content) {
        Question question = new Question();
        question.setUserId(UserHolder.getUserId());//当前用户id
        question.setTxt(content);//问题内容
        questionApi.saveQuestions(question);
    }


    /**
     * 通知设置 - 保存
     */
    public void saveSettings(SettingsVo settingsVo) {
        Settings settings = new Settings();
        //将settingsVo 数据复制到 settings
        BeanUtils.copyProperties(settingsVo, settings);
        settings.setUserId(UserHolder.getUserId());//当前用户id
        settingsApi.saveSettings(settings);
    }

    /**
     * 黑名单分页列表查询
     */
    public PageResult<UserInfoVoAge> blacklistPage(Long page, Long pagesize) {
        Long userId = UserHolder.getUserId();//当前用户id
        //1.调用服务分页查询黑名单列表数据
        PageResult<BlackList> blackListPageResult = blackListApi.blacklistPage(page, pagesize,userId);
        if(blackListPageResult == null || CollectionUtils.isEmpty(blackListPageResult.getItems())){
            return new PageResult<>(0l,10l,0l,1l,null);
        }
        List<BlackList> blackListList = blackListPageResult.getItems();
        //定义List<Long>接收黑名单ids
        List<Long> blackListIds = new ArrayList<>();
        //2.循环遍历黑名单列表获取出黑名单用户ids
        for (BlackList blackList : blackListList) {
            blackListIds.add(blackList.getBlackUserId());
        }
        //3.根据黑名单用户ids调用服务查询黑名单用户集合数据
        List<UserInfo> userInfoList = userInfoApi.findByBatchIds(blackListIds);
        List<UserInfoVoAge> userInfoVoAgeList = new ArrayList<>();
        for (UserInfo userInfo : userInfoList) {
            UserInfoVoAge userInfoVoAge = new UserInfoVoAge();
            BeanUtils.copyProperties(userInfo,userInfoVoAge);//用户id 头像 昵称 性别 年龄
            userInfoVoAgeList.add(userInfoVoAge);
        }
        //4.构造PageResult对象返回
        PageResult<UserInfoVoAge> infoVoAgePageResult = new PageResult<>();
        BeanUtils.copyProperties(blackListPageResult,infoVoAgePageResult);//将黑名单分页数据 copy vo分页对象中
        infoVoAgePageResult.setItems(userInfoVoAgeList);
        return infoVoAgePageResult;
    }

    /**
     * 黑名单 - 移除
     */
    public void removeBlackList(Long blackUserId) {
        blackListApi.removeBlackList(blackUserId,UserHolder.getUserId());
    }
}
